<?php

namespace app\common\model;

use app\admin\model\Options;
use think\Model;


class Question extends Model
{

    

    

    // 表名
    protected $name = 'question';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
//        'type_text',
//        'status_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getTypeList()
    {
        return ['radio' => __('Type radio'), 'checkbox' => __('Type checkbox')];
    }

    public function getStatusList()
    {
        return ['hidden' => __('Status hidden'), 'normal' => __('Status normal')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function project()
    {
        return $this->belongsTo('Project', 'project_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    /**
     * Account:查询选择题目
     * User: Ixiangang
     * Date: 2025/1/13
     * Time: 18:07
     */
    public static function isAnswerRight($question_id, $answer_id, $project_id)
    {
        # 测评类型
        $project_type = Project::where('id',$project_id)->value('type');
        if (!$project_type){
            throw_validate_error(!$project_type, "测评项目不存在");
        }
        $rightAnswer = \app\common\model\Options::where(['id' => $answer_id,'question_id' => $question_id])->find();
        if (empty($rightAnswer)){
            throw_validate_error(!$rightAnswer, "提交答案不存在");
        }
        $data = [
            'tag' => $rightAnswer->tag,
            'title' => $rightAnswer->title,
            'score' => $rightAnswer->score,
            'trigger_value' => $rightAnswer->trigger_value,
            'trigger_type' => $rightAnswer->trigger_type,
            'project_type' => $project_type
        ];
        return $data;
    }



}
